import {createRouter,createWebHashHistory} from 'vue-router'
import useStore from '@/store'
// 布局组件不用路由懒加载  路由懒加载也叫异步组件
import Layout from '@/views/layout/index.vue'
import Home from '@/views/home/index.vue'
const router=createRouter({
    // 匹配模式
    history:createWebHashHistory(),
    // 页面滚到顶部
    scrollBehavior:()=>{
        return{
            top:0
        }
    },
    // 路由规则
    routes:[
        {path:'/',component:Layout,
        children:[
            // 首页不用懒加载
            {path:'',component:Home},
            // 一级路由
            {path:'category/:id',component:()=>import('@/views/category/index.vue')},
            // 二级路由
            {path:'category/sub/:id',component:()=>import('@/views/category/sub.vue')},
            // 商品详情页
            {path:'goods/:id',component:()=>import('@/views/goods/index.vue')},
            //  购物车
            {path:'/cart',component:()=>import('@/views/cart/index.vue')},
            // 结算
            {path:'/member/checkout',component:()=>import('@/views/member/pay/checkout/index.vue')}

        ]},
        {path:'/login',component:()=>import('@/views/login/index.vue')},//路由懒加载
        {path:'/login/callback',component:()=>import('@/views/login/callback.vue')},//第三方回跳
        {path:'/demo',component:()=>import('@/views/demo/index.vue')},//测试用的
        
    ]
})

// 路由前置守卫
router.beforeEach((to,form,next)=>{
    const {cart}=useStore()
       if(cart.isLogin){
            next()
       }else{
        // 拦截没有登录去结算页
         if(to.path.includes('/member')){
            // next('/login') 用户携带参数跳转登录页 登录成功后跳转结算页
            next({
                path:'/login',
                query:{
                    redirectUrl:to.fullPath
                }
            })
         }else{
            next()
         }
       }
})









export default router